package com.yhyvr.api.controller.captcha;

import com.google.common.collect.Maps;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.lz.kit.LogKit;
import com.lz.kit.PropKit;
import com.yhyvr.api.handle.ApiData;
import com.yhyvr.pojo.req.api.ApiReqData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pub.greenbamboo.captcha.ACaptcha;
import pub.greenbamboo.captcha.ImageCode;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.Map;

/**
 * Created by tech6 on 2016/9/14.
 * 各种码获取
 */

@Controller
public class CaptchaController {
    private static final String f;
    static {
        f= PropKit.use("three-app.properties").get("sys.captcha");
    }
    @RequestMapping("/api/captcha/get/{id}")
    public @ResponseBody  void  getCaptchaImage(@PathVariable String id, HttpServletResponse response, HttpServletRequest httpServletRequest){
        if(f.indexOf(id)!=-1) {
            OutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
                ACaptcha instance = new ACaptcha();
                ImageCode result = instance.getImageCode();
                httpServletRequest.getSession().setAttribute(id, result.getCode());
                ImageIO.write((RenderedImage) result.getImage(), "png", outputStream);
            } catch (IOException e) {
                LogKit.info(e.getMessage());
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                        outputStream.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *邀请二维码获取
     */
    @RequestMapping("/api/qrcode/make")
    public @ResponseBody void qrcode(String content,HttpServletResponse response){
        OutputStream outputStream=null;
        try {
            outputStream=response.getOutputStream();
            String format = "png";// 图像类型
            Map<EncodeHintType, Object> hints = Maps.newHashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            String cont =URLDecoder.decode(content,"utf-8");
            cont=cont.replace(".jpg","");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(cont, BarcodeFormat.QR_CODE, 400, 400, hints);
            MatrixToImageWriter.writeToStream(bitMatrix,format,outputStream);// 输出图像
        } catch (IOException e) {
            LogKit.error(e.getMessage(),e);
        } catch (WriterException e) {
            LogKit.error(e.getMessage(),e);
        } catch (Exception e) {
            LogKit.error(e.getMessage(),e);
        } finally{
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                LogKit.error(e.getMessage(),e);
            }
        }
    }

    /**
     *邀请二维码获取
     */
    @RequestMapping("/api/qrcode/make/{content}")
    public @ResponseBody void qrcode2(@PathVariable String content,HttpServletResponse response){
        OutputStream outputStream=null;
        try {
            outputStream=response.getOutputStream();
            String format = "png";// 图像类型
            Map<EncodeHintType, Object> hints = Maps.newHashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            String cont =URLDecoder.decode(content,"utf-8");
            cont=cont.replace(".jpg","");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(cont, BarcodeFormat.QR_CODE, 400, 400, hints);
            MatrixToImageWriter.writeToStream(bitMatrix,format,outputStream);// 输出图像
        } catch (IOException e) {
            LogKit.error(e.getMessage(),e);
        } catch (WriterException e) {
            LogKit.error(e.getMessage(),e);
        } catch (Exception e) {
            LogKit.error(e.getMessage(),e);
        } finally{
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                LogKit.error(e.getMessage(),e);
            }
        }
    }
}
